/**
 * Created by sxx on 2017/5/3.
 */
import "./index.less";
import productListTpl from "./product_list.handlebars";
import paginationTpl from "./paging.handlebars";

let token = sessionStorage.getItem('supplier_token');

var InviteBiddingList = {
    pageNo: 1,
    pageSize: 20,
    maxPage: 1,
    isFirstPage: false,
    isLastPage: false,
    total: 0,

    init: function() {
        // this.test();
        this.getData()
            .then(this.renderProduct)
            .then(this.renderPagination)
            .then(this.bindEvent);

    },

    // 获取自荐商品
    getData: function() {
        return new Promise((resolve, reject) => {
            let self = this;
            $.ajax({
                url: HOST_ADDR + `/cpmp/splrSlfMng/chrm/`,
                type: 'get',
                dataType: 'json',
                headers: {
                    'access_token': token,
                    'login_type': 'splr'
                },
                contentType: "application/json",
                success: function(resp) {
                    if (resp.success) {
                        console.log(resp);
                        self.total = resp.data.count;
                        self.isFirstPage = self.pageNo === 1;
                        self.maxPage = Math.ceil(self.total / self.pageSize);
                        if (self.pageNo * self.pageSize >= self.total) {
                            self.isLastPage = true;
                        } else {
                            self.isLastPage = false;
                        }

                        resolve(resp);
                    } else {
                        alert(resp.msgInf);
                    }
                },
                error: function(err) {
                    console.log(err);
                }
            });

        });
    },

    // 渲染产品列表
    renderProduct: function(resp) {
        $.each(resp.data.list,function(index,item){
            item.chrmAddr = HOST_ADDR+'/'+`${item.chrmIcon}`;
        })
        let productHtml = productListTpl({ productList: resp.data.list });

        $('.J_product-list').html(productHtml);
    },

    // 渲染分页
    renderPagination: function() {
        let self = InviteBiddingList;
        let pageList = [];
        for (let i = 0; i < self.maxPage; i++) {
            pageList.push({ idx: i + 1 });
        }
        let paginationHtml = paginationTpl({
            begin: (self.pageNo - 1) * self.pageSize + 1,
            end: self.isLastPage ? self.total : self.pageNo * self.pageSize,
            total: self.total,
            isLastPage: self.isLastPage,
            isFirstPage: self.isFirstPage,
            pageList: pageList

        });
        $('.J_pagination').html(paginationHtml);
        $('.page-item').each((index, item) => {
            if ($(item).data('index') === self.pageNo) {
                console.log(this);
                $(item).addClass('active');
            }
        });
    },

    // 绑定事件
    bindEvent: function() {
        let self = InviteBiddingList;
        $('.J_prev').unbind().click(function() {
            if (self.pageNo > 1) {
                self.pageNo = self.pageNo - 1;
                self.renderList();
            }
        });
        $('.J_next').unbind().click(function() {
            if (self.pageNo < self.maxPage) {
                self.pageNo = self.pageNo + 1;
                self.renderList();
            }

        });
        $('.page-item').unbind().click(function() {
            let $item = $(this);

            if (!$item.hasClass('active')) {
                self.pageNo = $item.data('index');
                self.renderList();
            }
        });
        $('.J_todo').unbind().click(function() {
            alert('技术大大们正在抓紧开发中，请稍候...');
        });
        $('.J_delete').unbind().click(function() {
            let prodId = $(this).data('id');

            $(function() {
                $('#myModal').modal('show');
                $('.J_ensure_delete').unbind().click(function() {
                    $('#myModal').modal('hide');
                    $.ajax({
                        url: HOST_ADDR + '/cpmp/splrSlfMng/delChrm/' + prodId,
                        type: 'post',
                        dataType: 'json',
                        contentType: "application/json",
                        access_token: token,
                        headers: {
                            'access_token': token,
                            'login_type': 'splr'
                        },
                        success: function(resp) {
                            if (resp.success && resp.msgCod === 'S0002') {
                                alert('删除供应商风采成功');
                                location.reload();
                            } else {
                                alert('删除供应商风采失败.' + resp.msgCod + ' ' + resp.msgInf);
                            }
                        },
                        error: function(err) {
                            console.log(err);
                        }
                    });
                });

            });


        });
    },

    // 渲染列表
    renderList() {
        this.getData()
            .then(this.renderProduct)
            .then(this.renderPagination)
            .then(this.bindEvent);
    }

};

jQuery(document).ready(function() {
    App.init();
    InviteBiddingList.init();

});